import pymysql
from bs4 import BeautifulSoup
import app
import json
import logging


# 构建测试数据
# 数据格式：[(), (), ()]
# 数据格式：[[], [], []]
# "num,status_code"
def build_data(data_file_name, keys):
    test_data = []
    with open(app.BASE_DIR + "/data/" + data_file_name, encoding="UTF-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            test_case_data = []
            for key in keys.split(","):
                value = case_data.get(key)
                test_case_data.append(value)
            test_data.append(test_case_data)
    logging.info("test_data={}".format(test_data))
    return test_data


# 封装通用的断言方法
def common_assert(test_case, response, status_code, status, description):
    test_case.assertEqual(status_code, response.status_code)
    test_case.assertEqual(status, response.json().get("status"))
    if description is not None:
        test_case.assertIn(description, response.json().get("description"))


# 提取form数据
def parse_form_data(html):
    soup = BeautifulSoup(html, "html.parser")
    action = soup.form["action"]
    print("action====", action)
    # 获取所有的input标签
    form_param = {}
    for input in soup.find_all("input"):
        name = input["name"]
        value = input["value"]
        print("name={}  value={}".format(name, value))
        form_param[name] = value
    print("form_param===", form_param)

    return action, form_param


class DBUtil:
    _conn = None  # 数据库连接对象

    @classmethod
    def get_conn(cls):
        """获取数据库连接对象"""
        if cls._conn is None:
            cls._conn = pymysql.connect(host="121.43.169.97", user="root", password="Itcast_p2p_20191228",
                                        database="czbk_member", autocommit=True)
        return cls._conn

    @classmethod
    def close_conn(cls):
        """关闭数据库连接"""
        if cls._conn:
            cls._conn.close()
            cls._conn = None

    @classmethod
    def get_cursor(cls):
        """获取游标对象"""
        return cls.get_conn().cursor()

    @classmethod
    def close_cursor(cls, cursor):
        """关闭游标对象"""
        if cursor:
            cursor.close()

    @classmethod
    def get_one(cls, sql):
        """查询一条记录"""
        data = None
        cursor = None
        try:
            cursor = cls.get_cursor()
            cursor.execute(sql)
            data = cursor.fetchone()
        except Exception as e:
            print("get_one error: ", e)
        finally:
            cls.close_cursor(cursor)
            cls.close_conn()
        return data

    @classmethod
    def execute(cls, sql):
        """执行新增、修改、删除的sql语句"""
        cursor = None
        try:
            cursor = cls.get_cursor()
            cursor.execute(sql)
        except Exception as e:
            print("execute error: ", e)
        finally:
            cls.close_cursor(cursor)
            cls.close_conn()

    def __del__(self):
        print("del........")




if __name__ == '__main__':
    r = DBUtil.get_one("select 1")
    print("r=========", r)

